package divisibilityby8;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DivisibilityBy8 {
	Set<Integer> multiplesOfEightAll = null;
    
	public DivisibilityBy8() {
		multiplesOfEightAll = new HashSet<Integer>();
        for(int i = 104; i < 1000; i+=8){
        	multiplesOfEightAll.add(i);
    	}
    }

    public static void main(String[] args) {
            DivisibilityBy8 div = new DivisibilityBy8();
//            String value = "123";
//            char[] digits = value.trim().toCharArray();
//            System.out.println(div.isDivisibleBy8ForDigit3(digits));
              System.out.println(div.isDivisibleByEightForDigit3("1112"));
            
    }
    
    public boolean isDivisibleByEightForDigit3(String inputNumber){
    	boolean isDivisible = false;
    	List<Integer> digitList = new ArrayList<Integer>(); 
    	for(Integer i : multiplesOfEightAll){
    		String value = i.toString();
    		char[] digits = value.toCharArray();
    		
    		for(char c : digits){
    			int index = inputNumber.indexOf(c);
    			while(index >=0){
	    			if(!digitList.contains(index)){
	    				digitList.add(index);
	    				break;
	    			}  
	    			index = inputNumber.indexOf(c, index+1);
    			}
    		}
    		if(digitList.size() == 3){
    			isDivisible = true;
    			break;
    		}
    		digitList.clear();
    	}
    	return isDivisible;
    }
    public boolean isDivisibleBy8ForDigit3(char[] digits){
           
            String value = "";
            boolean isDivisible = false;
            for(int i = 0; i < digits.length;i ++){
                    for(int j = 0;j< digits.length; j++){
                            if(j == i ){
                                    continue;
                            }
                            for(int k = 0; k < digits.length; k++){
                                    if(k == i || k == j){
                                            continue;
                                    }
                                    value = digits[i] +""+ digits[j] + ""+digits[k];
                                    int intValue = Integer.parseInt(value);        
                                    System.out.println(intValue);
                                    if((intValue%8) == 0){
                                            isDivisible = true;
                                            break;
                                    }
                            }
                            if(isDivisible)break;
                    }
                    if(isDivisible)break;
            }
            return isDivisible;
    }
    public boolean isDivisibleBy8ForDigit2(char[] digits){
           
            String value = "";
            boolean isDivisible = false;
            for(int i = 0; i < digits.length;i ++){
                    for(int j = 0;j< digits.length; j++){
                            if(j == i ){
                                    continue;
                            }
                            value = digits[i] +""+ digits[j];
                            int intValue = Integer.parseInt(value);
                            if((intValue%8) == 0){
                                    isDivisible = true;
                                    break;
                            }
                           
                    }
                    if(isDivisible)break;
            }
            return isDivisible;
    }
    
//  private void permutate(){
//          for(int i = )
//  }
//  int[][] digit3Permutation = {{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,1,0},{2,0,1}};
}
